package com.soccertgc.comm
{
	import com.soccertgc.utils.ClassUtils;
	
	public class CallStatus
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const READY:CallStatus					= new CallStatus(0, "READY");					// ready to be executed
		public static const EXECUTING:CallStatus				= new CallStatus(1, "EXECUTING");				// call send, waiting response
		public static const EXECUTED_WITH_SUCCESS:CallStatus	= new CallStatus(2, "EXECUTED_WITH_SUCCESS");	// response received, call executed with success
		public static const EXECUTED_WITH_ERROR:CallStatus		= new CallStatus(3, "EXECUTED_WITH_ERROR");		// response received, call executed with an handled error
		public static const FAILED:CallStatus					= new CallStatus(4, "FAILED");					// response not received cause a communication or internal error 
		
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<CallStatus>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<CallStatus>();
			_list.push(CallStatus.READY);
			_list.push(CallStatus.EXECUTING);
			_list.push(CallStatus.EXECUTED_WITH_SUCCESS);
			_list.push(CallStatus.FAILED);
			_list.push(CallStatus.EXECUTED_WITH_ERROR);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function CallStatus(id:uint, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("CallStatus enum already created!");
			// Init
			_id    = id;
			_name  = name;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names for all possible the values of CallStatus enumeration
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * Returns all possible the values for the CallStatus enumeration
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a CallStatus by given id 
		 * @param id Id of the card type to find
		 * @return PlayerCallStatus constant for the given id, null if no CallStatus
		 * for the given id has been found
		 */		
		public static function getById(id:uint):CallStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("CallStatus for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a CallStatus by given name 
		 * @param name Name of the player CallStatus to find
		 * @return PlayerCallStatus constant for the given id, null if no CallStatus
		 * for the given name has been found
		 */		
		public static function getByName(name:String):CallStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("CallStatus for name '" + name + "' unknown!");
		}
		
	}
}